package com.abe.libquick.utils.quick;

import android.text.TextUtils;
import android.widget.CheckBox;

import com.abe.libquick.utils.Annotation.Parent;
import com.abe.libquick.utils.Annotation.SortableField;

import java.util.List;

@SuppressWarnings({"rawtypes", "unchecked"})
public class CheckBoxUtils {
    public static String getCBox(CheckBox... temp) {
        String str = "";
        for (CheckBox btn : temp) {
            if (btn.isChecked()) {
                if (!TextUtils.isEmpty(str)) {
                    str += ",";
                }
                str += btn.getText().toString();
            }
        }
        return str;
    }

    public static String getCBoxTag(CheckBox... temp) {
        String str = "";
        for (CheckBox btn : temp) {
            if (btn.isChecked()) {
                if (!TextUtils.isEmpty(str)) {
                    str += ",";
                }
                str += btn.getTag().toString();
            }
        }
        return str;
    }

    public static void setCBoxTag(String strArray, CheckBox... temp) {
        for (CheckBox btn : temp) {
            if (strArray != null && !strArray.equals("")) {
                for (String str : strArray.split(",")) {
                    if (btn.getTag().toString().equals(str)) {
                        btn.setChecked(true);
                    }
                }
            }
        }
    }

    public static void setCBox(String strArray, CheckBox... temp) {
        for (CheckBox btn : temp) {
            if (strArray != null && !strArray.equals("")) {
                for (String str : strArray.split(",")) {
                    if (btn.getText().toString().equals(str)) {
                        btn.setChecked(true);
                    }
                }
            }
        }
    }

    public static void setCBox(CheckBox temp[], String strArray) {
        for (CheckBox btn : temp) {
            if (!TextUtils.isEmpty(strArray)) {
                for (String str : strArray.split(",")) {
                    if (btn.getText().toString().equals(str)) {
                        btn.setChecked(true);
                    }
                }
            }
        }
    }

    public static <T> void setCBoxPrint(Class<T> clazz, Object obj, CheckBox temp[], String strArray) {
        if (!TextUtils.isEmpty(strArray)) {
            Parent a = new Parent<>(clazz);
            List<SortableField> list = a.init();
            for (CheckBox btn : temp) {
                String btnName = btn.toString().substring(
                        btn.toString().indexOf("/") + 1,
                        btn.toString().indexOf("}"));
                for (SortableField l : list) {
                    if (l.getName().equals(btnName)) {
                        for (String str : strArray.split(",")) {
                            if (str.equals(btn.getText().toString())) {
                                try {
                                    l.getField().set(obj, "On");
                                } catch (IllegalArgumentException e) {
                                    e.printStackTrace();
                                } catch (IllegalAccessException e) {
                                    e.printStackTrace();
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}